/** \file    throwWin32Error.h
  * \brief   Call this function to throw std::exception subclass after unsuccessful Win32 function call.
  * \date    2002
  * \author  Fedor Chelnokov
  * \version $Header: /home/cscom1r4/cvsroot/cs/include/throwWin32Error.h,v 1.1 2004/05/11 14:56:23 cscom1r4 Exp $
  */

#ifndef _throwWin32Error_h_
#define _throwWin32Error_h_

#define WIN32_LEAN_AND_MEAN 
#include "windows.h"
#include <xtd/exception>
#include <string>

struct Win32Error : xtd::exception 
{
	Win32Error(const std::string & suffix = std::string()) throw()
	{
		char * buf;
		if (FormatMessage(
			FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, 0, 
			GetLastError(), 0, (LPTSTR)&buf, 0, 0)) 
		{
			message(std::string("System error: ") + buf + suffix);
			LocalFree(buf);
		}
	}
};

inline void throwWin32Error(const std::string & executedFunc)
{
	throw Win32Error(std::string("\nas a result of ") + executedFunc);
}

#endif //_throwWin32Error_h_
